<template>
  <div class="app-container">
    <el-form class="base-search-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户编号" prop="platformUserId">
        <el-input
          v-model="queryParams.platformUserId"
          placeholder="请输入用户编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择用户状态" clearable>
          <el-option
            v-for="dict in plat_user_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="拉黑时间">
        <el-date-picker clearable
          v-model="searchDate"
          type="daterange"
          value-format="YYYY-MM-DD"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          @change="confirmSearchDate">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="base-container mb-50px">
      <el-row :gutter="10" class="mb-10px">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['platuser:platUserBlackList:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['platuser:platUserBlackList:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table border v-loading="loading" :data="platUserBlackListList">
        <el-table-column label="用户名称" align="center" prop="userName" min-width="120" show-overflow-tooltip />
        <el-table-column label="手机号码" align="center" prop="mobile" min-width="120" />
        <el-table-column label="用户状态" align="center" prop="status" min-width="100">
          <template #default="scope">
            <dict-tag :options="plat_user_status" :value="scope.row.status" @click="userStateClick"/>
          </template>
        </el-table-column>
        <el-table-column label="来源渠道" align="center" min-width="150" prop="channelName" show-overflow-tooltip />
        <!-- <el-table-column label="异常订单数" align="center" prop="exOrderNum" min-width="100" /> -->
        <!-- <el-table-column label="拉黑状态" align="center" prop="userState" min-width="100">
          <template #default="scope">
            <dict-tag :options="bus_user_back_list" :value="scope.row.userState"/>
          </template>
        </el-table-column> -->
         <el-table-column label="用户编号" align="center" prop="platformUserId" min-width="160" show-overflow-tooltip />
        <el-table-column label="拉黑时间" align="center" prop="blackTime" min-width="150">
          <template #default="scope">
            <span>{{ parseTime(scope.row.blackTime, '{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="拉黑原因" align="center" prop="remark" min-width="150" show-overflow-tooltip/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="80">
          <template #default="scope">
            <el-tooltip content="解封" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['platuser:platUserBlackList:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </div>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改黑名单对话框 -->
    <Dialog v-model="showDialog" :title="dialogTitle">
      <edit-black 
        ref="editBlack"
        @cancel="cancel"
        @confirm="confirm"
      />
    </Dialog>
  </div>
</template>

<script setup name="BlackList">
import { listPlatUserBlackList, delPlatUserBlackList } from "@/api/user/blackList"
import { nextTick } from "vue"
import EditBlack from './edit.vue'

const { proxy } = getCurrentInstance()
const { bus_user_black_list, plat_user_status } = proxy.useDict('bus_user_black_list', 'plat_user_status')

const editBlack = ref()
const platUserBlackListList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const showDialog = ref(false)
const dialogTitle = ref('')
const searchDate = ref([])

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    platformUserId: null,
    userName: null,
    mobile: null,
    status: null,
    beginBlackTime: null,
    endBlackTime: null
  }
})

const { queryParams } = toRefs(data)

const userStateClick = (obj) => {
  queryParams.value.status = obj.value
  handleQuery()
}

const confirmSearchDate = (e) => {
  if (e && e.length) {
    queryParams.value.beginBlackTime = e[0]
    queryParams.value.endBlackTime = e[1]
  } else {
    queryParams.value.beginBlackTime = null
    queryParams.value.endBlackTime = null
  }
  handleQuery()
}

/** 查询黑名单列表 */
function getList() {
  loading.value = true
  listPlatUserBlackList(queryParams.value).then(response => {
    platUserBlackListList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  showDialog.value = false
  dialogTitle.value = ''
}

function confirm() {
  cancel()
  getList()
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  searchDate.value = []
  queryParams.value.beginBlackTime = null
  queryParams.value.endBlackTime = null
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 新增按钮操作 */
function handleAdd() {
  showDialog.value = true
  dialogTitle.value = '添加黑名单'
}

/** 移出按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认解封该用户').then(function() {
    return delPlatUserBlackList(row.id)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("解封成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('platuser/platUserBlackList/export', {
    ...queryParams.value
  }, `黑名单_${proxy.getFileImportName()}.xlsx`)
}

getList()
</script>
